package net.sppan.base.dao;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import net.sppan.base.dao.support.IBaseDao;
import net.sppan.base.entity.ContentExuser;
import net.sppan.base.entity.DyContexts;
import net.sppan.base.entity.ExContentView;
@Repository
@Transactional(isolation = Isolation.READ_COMMITTED)
public interface IContentExuserDao extends IBaseDao<ContentExuser, Long>{
	
	@Query(value="select new net.sppan.base.entity.DyContexts ("
			+ " e.dyContentId  ,e.publisherDyId  ,e.uploadDate , "
			+ " e.contentText ,e.contentUrl,e.videoDisplay , c.pixelWidth , "
			+ " c.pixelHeight ,c.duration ,c.fileSize, e.contentType, c.praiseNum,c.trampleNum ,ui.nickName , ui.headPortraitUrl  ) "
			+ "from  ContentExuser e , ContentUnreviewed c ,DyUserInfo ui "
			+ "where  e.dyContentId =c.dyContentId and c.publisherDyId=ui.dyId "
			+ "and e.exdyId=:dyId and e.exResult=0 and e.dyContentId < :countentId "
			+ "ORDER  BY e.uploadDate DESC  "
			,countQuery =" select count(*)  from  ContentExuser e , ContentUnreviewed c ,DyUserInfo ui where  e.dyContentId =c.dyContentId and c.publisherDyId=ui.dyId  and e.exdyId=:dyId and e.exResult=0 and e.dyContentId < :countentId ")
	public Page<DyContexts> findContentExuserByDyId(@Param("dyId")String dyId,@Param("countentId") Long countentId , Pageable pageable);
	
	@Query(value="select new net.sppan.base.entity.ExContentView( e.id , e.exuserId , e.dyContentId as dyContentId, "
		    + "e.exResult, e.exTime, e.exDesc, c.publisherDyId, "
		    + "c.uploadDate, c.contentText, c.contentUrl, "
		    + "c.videoDisplay, c.pixelWidth, c.pixelHeight, "
		    + "c.duration, c.fileSize, c.praiseNum, c.trampleNum, "
		    + "c.contentType, c.isApproved ,u.nickName , u.headPortraitUrl ) "
			+ "from  ContentExuser e , ContentUnreviewed c ,DyUserInfo u "
			+ "where  e.dyContentId =c.dyContentId and c.publisherDyId=u.dyId "
			+ "and e.exuserId=:userId and e.exResult=0 "
			+ "ORDER  BY e.uploadDate DESC  "
			,countQuery =" select count(*)  from  ContentExuser e , ContentUnreviewed c ,DyUserInfo u where  e.dyContentId =c.dyContentId and c.publisherDyId=u.dyId and e.exuserId=:userId and e.exResult=0  ")
	public  Page<ExContentView> findContentExuserByUserId(@Param("userId")Long userId, Pageable pageable);
	
	
	@Modifying
	@Query(value ="update ContentExuser set exResult=:exResult , allotTime=:allotTime , exuserId=:userId where id =:id")
	public void updateContentExuserDao(@Param("exResult")String exResult,@Param("userId")Long userId,@Param("allotTime")Date allotTime,@Param("id")Long id);


	@Modifying
	@Query(value ="update ContentExuser set exResult=:exResult , allotTime=:allotTime , exuserId=0 ,exdyId =:dyId where id =:id")
	public void updateContentDyIdDao(@Param("exResult")String exResult,@Param("dyId")String dyId,@Param("allotTime")Date allotTime,@Param("id")Long id);
	
	
	@Query(value ="FROM ContentExuser WHERE exResult=0 and exuserId=0 and  exdyId=:dyId and dyContentId=:contentId")
	public ContentExuser findByDyId(@Param("dyId")String dyId ,@Param("contentId")Long contentId);

	@Query(value ="select * from tr_comment_exuser where ", nativeQuery = true)
	public List<Object> contentCount(@Param("dyId")String dyId);
	
}
